
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.InputEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JOptionPane;


public class Kimp extends JFrame {        
    ArrayList<Point> points = new ArrayList<Point>();
    ArrayList<Line> lines = new ArrayList<Line>();
    static ArrayList<Tegevus> tegevused = new ArrayList<Tegevus>();
    static ArrayList<Figure> figures= new ArrayList<Figure>();
    static Figure curFig, selFig = null;
    static final int VIIS = 5;
        /**
    * @param args
         */
    public static void main(String[] args) {
    	Kimp k = new Kimp(600, 400);
    	k.repaint();
    }
    public Kimp(int width, int height) {
    	this.setSize(width, height);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.addMouseMotionListener(new MouseMotionAdapter() {
        	@Override
        	public void mouseDragged(MouseEvent e) {
	        	// TODO Auto-generated method stub
        			if (e.getModifiers() == InputEvent.BUTTON1_MASK + InputEvent.CTRL_MASK) { 
        				if (selFig != null) {
        					selFig.move(e.getPoint());
        				}
        				repaint();
        			} else if (e.getModifiers() == InputEvent.BUTTON1_MASK){
        				Point pnt = e.getPoint();
        				curFig.addLine(pnt);
        				repaint();
        			}
	        }
        	@Override
        	public void mouseMoved(MouseEvent e) {
        		//System.out.println(e.getModifiers() == InputEvent.CTRL_MASK);
        		if (e.getModifiers() == InputEvent.CTRL_MASK) {
        			Point p = e.getPoint();
        			selFig = selectFigure(p);
        			//if (selFig != null) { selFig.setColor(Color.RED); }
        			for (Figure each: figures) {
        				if (each != selFig) each.setColor(Color.BLACK);
        				else each.setColor(Color.RED);
        			}
        			repaint();
        		}
        	}
        });
        this.addMouseListener(new MouseAdapter() {
        	@Override
            public void mousePressed(MouseEvent e) {
        		// TODO Auto-generated metod stub
        		super.mousePressed(e);
        		if (e.getModifiers() == InputEvent.BUTTON1_MASK + InputEvent.CTRL_MASK){
        			if (selFig != null) { selFig.setMovePoint(e.getPoint()); }
        			repaint();
        		} else if (e.getModifiers() == InputEvent.BUTTON1_MASK) {
        			Point pnt = e.getPoint();
        			curFig = new Figure (pnt);
        			figures.add(curFig);
        			repaint();
        		} else if (e.getModifiers() == InputEvent.BUTTON3_MASK + InputEvent.CTRL_MASK) {
        			Tegevus delete = new Deleting(selFig);
        			tegevused.add(delete);
        			figures.remove(selFig);
        			selFig = null;
        			repaint();
        		}
        	}
        	public void mouseReleased(MouseEvent e) {
        		// TODO Auto-generated method stub
        		super.mouseReleased(e);
        		if (e.getModifiers() == InputEvent.BUTTON1_MASK){
        			Tegevus draw = new Drawing(curFig);
        			tegevused.add(draw);
        			points = new ArrayList<Point>();
        			repaint();
        		}
        		if (e.getModifiers() == InputEvent.BUTTON1_MASK + InputEvent.CTRL_MASK) {
        			if (selFig != null) {
        				selFig.pushPoint();
        				Tegevus move = new Moving (selFig);
        				tegevused.add(move);
        			}
        		}
        	}
        });
        this.addKeyListener(new KeyAdapter() {
        	@Override
            public void keyPressed(KeyEvent e) {
            // TODO Auto-generated method stub
        		super.keyPressed(e);                
        		if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
        			if (figures.size() == 0 && tegevused.size() == 0){
        				int result = JOptionPane.showConfirmDialog(null, "Are you sure?", "Exit?", JOptionPane.YES_NO_OPTION);
        				if (result == JOptionPane.YES_OPTION) {
        					System.exit(0);
        				}
        			} else { //UNDO!
        				System.out.println (tegevused.size());
        				tegevused.get(tegevused.size()-1).undo();
        				tegevused.remove(tegevused.size()-1);
        				repaint();
        			}
        		}
        	}
            public void keyReleased(KeyEvent e) {
                // TODO Auto-generated method stub
            	super.keyReleased(e);
            	if (e.getKeyCode() == KeyEvent.VK_CONTROL){ 
            		for (Figure each: figures) { each.setColor(Color.BLACK); }
            		repaint ();
            	}
            }
        });
    }
    public Figure selectFigure (Point p){
    	Figure ret = null;
    	float minDist = 0;
    	boolean first = true;
    	for (Figure each: figures){
    		if (first) {
    			ret = each;
    			first = false;
    			minDist = each.getMinDist(p);
    		}else {
    			if (each.getMinDist(p) < minDist) {
    				ret = each;
    				minDist = each.getMinDist(p);
    			}
    		}
    	}
    	if (minDist < VIIS) return ret;
    	else return null;
    }
    @Override
    public void paint(Graphics g) {
    	// TODO Auto-generated method stub
    	super.paint(g);
    	for (Figure eachFig: figures) {
    		for (Line eachLn: eachFig.getLines()) {
    			g.setColor(eachLn.color);
    			g.drawLine(eachLn.start.x, eachLn.start.y, eachLn.end.x, eachLn.end.y);
    		}
    	}
    }
}